package com.gxa.community.admin.controller.content;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gxa.community.admin.advice.Log;
import com.gxa.community.admin.service.content.AdService;
import com.gxa.community.core.pojo.domain.Ad;
import com.gxa.community.core.pojo.dto.content.PageAdDTO;
import com.gxa.community.core.utils.R;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/content")
public class AdvertisementController {
    @Autowired
    private AdService adService;

    @ApiOperation("获取所有广告")
    @GetMapping("/getalladvertisement")
    public R getAllAdvertisement(@RequestParam("currentPage")Integer currentPage, @RequestParam("pageSize")Integer pageSize){
        PageHelper.startPage(currentPage,pageSize);
        List<Ad> list = adService.list();
        if (!CollectionUtils.isEmpty(list)){
            PageInfo<Ad> pageInfo = new PageInfo<>(list);
            PageAdDTO pageAdDTO = new PageAdDTO();
            pageAdDTO.setTotal(pageInfo.getTotal());
            pageAdDTO.setPages(pageInfo.getPages());
            pageAdDTO.setCurrentPage(currentPage);
            pageAdDTO.setPageSize(pageSize);
            pageAdDTO.setAdvertisements(pageInfo.getList());
            return R.ok("查询成功！").put("data",pageAdDTO);
        }else {
            return R.error(600,"查询失败，请联系管理员！");
        }
    }

    @ApiOperation("根据广告名称搜索广告")
    @GetMapping("/searchadvertisementbyname")
    public R searchAdvertisementByName(@RequestParam("name")String name,@RequestParam("currentPage")Integer currentPage, @RequestParam("pageSize")Integer pageSize){
        if (StringUtils.isBlank(name)){
            //不输入则搜索全部
            return this.getAllAdvertisement(currentPage,pageSize);
        }else {
            PageHelper.startPage(currentPage,pageSize);
            //包装条件
            LambdaQueryWrapper<Ad> adLambdaQueryWrapper = Wrappers.lambdaQuery(Ad.class);
            adLambdaQueryWrapper.like(Ad::getName,name);
            List<Ad> list = adService.list(adLambdaQueryWrapper);
            if (!CollectionUtils.isEmpty(list)){
                PageInfo<Ad> pageInfo = new PageInfo<>(list);
                PageAdDTO pageAdDTO = new PageAdDTO();
                pageAdDTO.setTotal(pageInfo.getTotal());
                pageAdDTO.setPages(pageInfo.getPages());
                pageAdDTO.setCurrentPage(currentPage);
                pageAdDTO.setPageSize(pageSize);
                pageAdDTO.setAdvertisements(pageInfo.getList());
                return R.ok("查询成功！").put("data",pageAdDTO);
            }else {
                return R.error(600,"您欲查询的广告不存在！");
            }
        }
    }

    @ApiOperation("根据id查询单个广告")
    @GetMapping("/getsingleadvertisementbyid/{id}")
    public R getSingleAdvertisementById(@PathVariable("id")Integer id){
        LambdaQueryWrapper<Ad> adLambdaQueryWrapper = Wrappers.lambdaQuery(Ad.class);
        adLambdaQueryWrapper.eq(Ad::getId,id);
        Ad ad = adService.getOne(adLambdaQueryWrapper);
        return ad!=null?R.ok("查询成功！").put("data",ad):R.error(600,"查询失败，请联系管理员！");
    }

    @ApiOperation("新增广告")
    @PostMapping("/addadvertisement")
    @Log(title = "",action = "新增了广告")
    public R addAdvertisment(Ad ad){
        boolean saveResult = adService.save(ad);
        return saveResult?R.ok("添加成功！"):R.error(600,"添加失败，请联系管理员！！");
    }

    @ApiOperation("修改单个广告")
    @PutMapping("/updatesingleadvertisementinfo")
    @Log(title = "",action = "修改了广告")
    public R updateSingleAdvertisementInfo(Ad ad){
        boolean Result = adService.saveOrUpdate(ad);
        return Result?R.ok("修改成功！"):R.error(600,"修改失败，请联系管理员！！");
    }

    @ApiOperation("上线或下线广告接口")
    @PutMapping("/updateadvertisementstatus/{id}")
    @Log(title = "",action = "上线或下线广告")
    public R updateAdvertisementStatus(@PathVariable("id") Integer id){
        LambdaQueryWrapper<Ad> adLambdaQueryWrapper = Wrappers.lambdaQuery(Ad.class);
        adLambdaQueryWrapper.eq(Ad::getId,id);
        Ad ad = adService.getOne(adLambdaQueryWrapper);
        if (ad.getStatus()==1){
            ad.setStatus(0);
            boolean result = adService.saveOrUpdate(ad);
            return result?R.ok("修改成功！"):R.error(600,"修改失败，请联系管理员！！");
        }else {
            ad.setStatus(1);
            boolean result = adService.saveOrUpdate(ad);
            return result?R.ok("修改成功！"):R.error(600,"修改失败，请联系管理员！！");
        }
    }

    @ApiOperation("根据id删除广告接口")
    @DeleteMapping("/deleteadvertisementbyid/{id}")
    @Log(title = "",action = "删除了广告")
    public R delAdvertisementById(@PathVariable("id") Integer id){
        boolean result = adService.removeById(id);
        return result?R.ok("删除成功！"):R.error(600,"删除失败，请联系管理员！！");
    }

    /**
     * 远程通信接口,获取到所有未被禁用的广告
     * @return
     */
    @GetMapping("/querAd")
    public List<Ad> Ad(){
        log.info("远程通信querAd进来了");
        QueryWrapper <Ad> wrapper = new QueryWrapper<>();
        wrapper.eq("status",1);
        List<Ad> list = adService.list(wrapper);
        return list;
    }

}
